package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>  implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean register(User user) {
        if (StringUtils.isEmpty(user.getUName())) {
            throw new IllegalArgumentException("用户名不能为空");
        }

        // 检查用户名是否已存在
        if (userMapper.countByUsername(user.getUName()) > 0) {
            return false;
        }

        // 设置默认值
        user.setUIsAdmin(false);
        user.setUBalance(BigDecimal.ZERO); // 表示数值0

        return userMapper.insertUser(user) > 0;
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            return null;
        }

        // 验证密码
        if (!user.getUPassword().equals(password)) {
            return null;
        }

        return user;
    }

    @Override
    @Transactional
    public boolean deleteUser(Integer userId) {
        // 检查用户是否存在
        if (userMapper.selectById(userId) == null) {
            return false;
        }

        // 删除用户
        return userMapper.deleteById(userId) > 0;
    }

    @Override
    @Transactional
    public boolean updateUser(Integer userId, User updatedUser) {
        // 检查用户是否存在
        User existingUser = userMapper.selectById(userId);
        if (existingUser == null) {
            return false;
        }

        // 更新用户信息
        if (updatedUser.getUName() != null) {
            existingUser.setUName(updatedUser.getUName());
        }
        if (updatedUser.getUPassword() != null) {
            existingUser.setUPassword(updatedUser.getUPassword());
        }
        if (updatedUser.getUPhoneNumber() != null) {
            existingUser.setUPhoneNumber(updatedUser.getUPhoneNumber());
        }
        if (updatedUser.getUBio() != null) {
            existingUser.setUBio(updatedUser.getUBio());
        }

        return userMapper.updateUser(existingUser) > 0;
    }

    @Override
    public User getUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    @Transactional
    public boolean addBalance(Integer userId, BigDecimal amount) {
        if (userId == null || amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return false;
        }

        int result = userMapper.addBalance(userId, amount);
        return result > 0;
    }

    @Override
    @Transactional
    public boolean deductBalance(Integer userId, BigDecimal amount) {
        if (userId == null || amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return false;
        }

        // 检查用户余额是否足够
        BigDecimal currentBalance = getBalance(userId);
        if (currentBalance.compareTo(amount) < 0) {
            throw new RuntimeException("用户余额不足");
        }

        int result = userMapper.deductBalance(userId, amount);
        return result > 0;
    }

    @Override
    public BigDecimal getBalance(Integer userId) {
        User user = userMapper.selectById(userId);
        return user != null ? user.getUBalance() : BigDecimal.ZERO;
    }

    @Override
    @Transactional
    public boolean updatePassword(Integer userId, String oldPassword, String newPassword) {
        // 获取用户
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }

        // 验证原密码
        if (!user.getUPassword().equals(oldPassword)) {
            return false;
        }

        // 更新密码
        return userMapper.updateUserPassword(userId, newPassword) > 0;
    }

    @Override
    @Transactional
    public User addBalancePI(Integer userId, BigDecimal amount) {
        if (userId == null || amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("充值金额必须大于0");
        }

        // 更新余额
        int result = userMapper.rechargeBalance(userId, amount);
        if (result <= 0) {
            throw new RuntimeException("充值失败");
        }

        // 返回更新后的用户信息
        return userMapper.selectById(userId);
    }
}